package com.lzy.web.admin;

import com.lzy.po.Tag;
import com.lzy.po.Type;
import com.lzy.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

/**
 * @autor shkstart
 * @create 2021-01-06 10:53)
 */
@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private TagService tagService;

    @GetMapping("/tags")
    public String toTagsPage(@PageableDefault(size = 5,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable,
                             Model model){
       model.addAttribute("page",tagService.listTag(pageable));
        return "admin/tags";
    }


    @GetMapping("/tags/input")
    public String toInputPage(Model model){
        model.addAttribute("tag",new Tag());
        return "admin/tags-input";
    }

    @PostMapping("/tags")
    public String post(@Valid Tag tag, BindingResult result, RedirectAttributes attributes){
        //重复名称添加失败
        Tag tagName = tagService.getTagByName(tag.getName());
        if(tagName != null){
            result.rejectValue("name","nameError","名称重复,新增失败");
        }

        //后端验证结果
        if(result.hasErrors()){
            return "admin/tags-input";
        }

        Tag t = tagService.saveTag(tag);
        if(t == null){
            attributes.addFlashAttribute("message", "新增操作失败");
        }else{
            attributes.addFlashAttribute("message", "新增操作成功");
        }

        return "redirect:/admin/tags";
    }

    //编辑页面
    @GetMapping("tags/{id}/input")
    public String toEditPage(@PathVariable("id") Long id, Model model){
        model.addAttribute("tag",tagService.getTag(id));
        return "admin/tags-input";
    }

    //编辑
    @PostMapping("/tags/{id}")
    public String editPost(@Valid Tag tag, BindingResult result,
                           @PathVariable Long id,
                           RedirectAttributes attributes){
        //重复名称添加失败
        Tag tagName = tagService.getTagByName(tag.getName());
        if(tagName != null){
            result.rejectValue("name","nameError","名称重复,编辑失败");
        }

        //后端验证结果
        if(result.hasErrors()){
            return "admin/tags-input";
        }

        Tag t = tagService.updateTag(id,tag);
        if(t == null){
            attributes.addFlashAttribute("message", "更新操作失败");
        }else{
            attributes.addFlashAttribute("message", "更新操作成功");
        }

        return "redirect:/admin/tags";
    }

    //删除
    @GetMapping("/tags/{id}/delete")
    public String deleteType(@PathVariable Long id,RedirectAttributes attributes){
        tagService.deleteTag(id);
        attributes.addFlashAttribute("message","删除操作成功");
        return "redirect:/admin/tags";  //重定位要加/
    }
}
